package com.zlzsy.app.base.ui

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.support.v4.app.Fragment
import android.util.Log
import android.view.MenuItem
import android.widget.FrameLayout
import com.zlzsy.app.base.R
import com.zlzsy.app.base.ui.interfaces.IFragment
import org.jetbrains.anko.design.coordinatorLayout
import org.jetbrains.anko.frameLayout
import org.jetbrains.anko.matchParent

/**
 * Created by niantuo on 2017/11/6.
 * 加载fragment的Activity
 */
class AppFragmentActivity : BaseActivity() {

    companion object {
        private val TAG: String = AppFragmentActivity::class.java.simpleName
        private val KEY_CLASS = "KEY_FRAGMENT_CLASS"
        private val KEY_FULL_SCREEN = "KEY_FULL_SCREEN"
        fun <T : Fragment> startActivity(context: Context, clazz: Class<T>, fullScreen: Boolean = false) {
            val intent = Intent(context, AppFragmentActivity::class.java)
            intent.putExtra(KEY_CLASS, clazz)
            intent.putExtra(KEY_FULL_SCREEN, fullScreen)
            context.startActivity(intent)
        }

        fun <T : Fragment> startActivity(context: Activity, requestCode: Int, clazz: Class<T>, fullScreen: Boolean = false) {
            val intent = Intent(context, AppFragmentActivity::class.java)
            intent.putExtra(KEY_CLASS, clazz)
            intent.putExtra(KEY_FULL_SCREEN, fullScreen)
            context.startActivityForResult(intent, requestCode)
        }

        fun <T : Fragment> startActivity(context: Fragment, requestCode: Int, clazz: Class<T>, fullScreen: Boolean = false) {
            val intent = Intent(context.context, AppFragmentActivity::class.java)
            intent.putExtra(KEY_CLASS, clazz)
            intent.putExtra(KEY_FULL_SCREEN, fullScreen)
            context.startActivityForResult(intent, requestCode)
        }
    }


    lateinit var mContainer: FrameLayout
    lateinit var mFragment: Fragment

    override fun initView() {

        val fullScreen = intent.getBooleanExtra(KEY_FULL_SCREEN, false)
        if (fullScreen) {
            setTheme(R.style.AppTheme_FullScreenWithBar)
        }

        coordinatorLayout {
            mContainer = frameLayout {
                id = R.id.fgContainer
                lparams(matchParent, matchParent)
            }
        }

        // 获取Class类对象
        val cls = intent.getSerializableExtra(KEY_CLASS) as Class<*>
        mFragment = cls.getConstructor().newInstance() as Fragment

        Log.d(TAG, "fragment: $mFragment")
        supportFragmentManager.beginTransaction()
                .add(R.id.fgContainer, mFragment, mFragment.tag)
                .commitNow()

    }

    override fun onBackPressed() {
        if ((mFragment as? IFragment)?.onBackPressed() == true) {
            return
        }
        super.onBackPressed()
    }


    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        return if (mFragment.onOptionsItemSelected(item)) {
            true
        } else {
            super.onOptionsItemSelected(item)
        }
    }

}